﻿#include "processdatamanager.h"

ProcessDataManager::ProcessDataManager(QObject *parent)
    : QObject(parent)
{
    m_model = new ParameterModel(this);
}

bool ProcessDataManager::isExistsTable()
{
    ParameterSQLDB Db;
    QString sql = "SELECT ID, Name, Number, Knife, Roller, Stress01, Stress02, Stress03, F_Knife01, F_Knife02, F_Knife03, B_Knife01, B_Knife02, B_Knife03 FROM ProcessParameter;";

    qDebug()<<"Cpp:Item Sql:"<<sql;
    bool ok = Db.execSql(sql);
    if(!ok)
    {
        qDebug()<<"Cpp:不存在数据库表 ProcessParameter";

        sql = "CREATE TABLE ProcessParameter (ID INTEGER PRIMARY KEY ,Name TEXT ,Number TEXT ,Knife TEXT ,Roller TEXT ,Stress01 TEXT ,Stress02 TEXT ,Stress03 TEXT , \
                F_Knife01 TEXT ,F_Knife02 TEXT ,F_Knife03 TEXT ,B_Knife01 TEXT ,B_Knife02 TEXT ,B_Knife03 TEXT);";
        qDebug()<<"Cpp:创建数据表 ProcessParameter sql:"<<sql;
        bool ok = Db.execSql(sql);
        if(ok)
        {
            qDebug()<<"Cpp:创建数据表 ProcessParameter 成功";
        }
        else
        {
            qDebug()<<"Cpp:创建数据表 ProcessParameter 失败";
            return false;
        }
    }
    else
    {
        getAllParameterFromSQL();
    }

    return true;


    return true;
}

void ProcessDataManager::getAllParameterFromSQL()
{
    qDebug()<<"Cpp:获取全部数据";

    ParameterSQLDB Db;

    QSharedPointer<QSqlQuery> result;

    ParameterModel* itemModel = new ParameterModel();

    QString sql = "SELECT ID ,Name ,Number ,Knife ,Roller ,Stress01 ,Stress02 ,Stress03 ,F_Knife01 ,F_Knife02 ,F_Knife03 ,B_Knife01 ,B_Knife02 ,B_Knife03 FROM ProcessParameter;";

    qDebug()<<"itemModel sql"<<sql;

    result.reset(Db.execSql_result(sql));
    if(!result.isNull())
    {
        while(result->next())
        {
            ParameterItem* item = new ParameterItem();

            int id = result->value(0).toInt();
            item->setId(id);

            QString name = result->value(1).toString();
            item->setName(name);

            QString number = result->value(2).toString();
            item->setNumber(number);

            QString knife = result->value(3).toString();
            item->setKnife(knife);

            QString roller = result->value(4).toString();
            item->setRoller(roller);

            QString stress01 = result->value(5).toString();
            item->setStress01(stress01);

            QString stress02 = result->value(6).toString();
            item->setStress02(stress02);

            QString stress03 = result->value(7).toString();
            item->setStress03(stress03);

            QString f_knife01 = result->value(8).toString();
            item->setF_Knife01(f_knife01);

            QString f_knife02 = result->value(9).toString();
            item->setF_Knife02(f_knife02);

            QString f_knife03 = result->value(10).toString();
            item->setF_Knife03(f_knife03);

            QString b_knife01 = result->value(11).toString();
            item->setB_Knife01(b_knife01);

            QString b_knife02 = result->value(12).toString();
            item->setB_Knife02(b_knife02);

            QString b_knife03 = result->value(13).toString();
            item->setB_Knife03(b_knife03);

            itemModel->addItem(item);
        }

        emit getAllParameter_Signal(itemModel);

        result.clear();
    }
}

void ProcessDataManager::deleteDataByID(int id)
{
    ParameterSQLDB Db;

    QSharedPointer<QSqlQuery> result;

    QString sql = "DELETE FROM ProcessParameter WHERE ID = "+QString::number(id);

    qDebug()<<"Cpp:Item sql:"<<sql;

    result.reset(Db.execSql_result(sql));

    bool ok = Db.execSql(sql);

    if(!ok)
    {
        qDebug()<<"Cpp:删除数据失败";
    }
    else
    {
        qDebug()<<"Cpp:删除数据成功";
        getAllParameterFromSQL();
    }
}

void ProcessDataManager::addData()
{
    ParameterSQLDB Db;

    QString sql = "INSERT INTO ProcessParameter (Name ,Number ,Knife ,Roller ,Stress01 ,Stress02 ,Stress03 ,F_Knife01 ,F_Knife02 ,F_Knife03 ,B_Knife01 ,B_Knife02 ,B_Knife03)\
        VALUES('','','','','','','','','','','','','')";

    qDebug()<<"Cpp:插入数据 sql:"<<sql;

    bool ok = Db.execSql(sql);
    if(!ok)
    {
        qDebug()<<"Cpp:添加数据失败";
    }
    else
    {
        qDebug()<<"Cpp:添加数据成功";
        getAllParameterFromSQL();
    }
}

void ProcessDataManager::uploadCrrtLocatoinData(QString knifeCrrt,QString rollerCrrt,
                                                QString stress01Crrt,QString stress02Crrt,QString stress03Crrt,
                                                QString f_knife01Crrt,QString f_knife02Crrt,QString f_knife03Crrt,
                                                QString b_knife01Crrt,QString b_knife02Crrt,QString b_knife03Crrt)
{
    ParameterSQLDB Db;

    QString sql = QString("INSERT INTO ProcessParameter (Name ,Number ,Knife ,Roller ,Stress01 ,Stress02 ,Stress03 ,F_Knife01 ,F_Knife02 ,F_Knife03 ,B_Knife01 ,B_Knife02 ,B_Knife03)\
                  VALUES('','','%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11')")
                      .arg(knifeCrrt).arg(rollerCrrt).arg(stress01Crrt).arg(stress02Crrt).arg(stress03Crrt)
                        .arg(f_knife01Crrt).arg(f_knife02Crrt).arg(f_knife03Crrt).arg(b_knife01Crrt).arg(b_knife02Crrt).arg(b_knife03Crrt);

                  qDebug()<<"Cpp:插入上载数据 sql:"<<sql;

    bool ok = Db.execSql(sql);
    if(!ok)
    {
        qDebug()<<"Cpp:上载数据失败";
    }
    else
    {
        qDebug()<<"Cpp:上载数据成功";
        getAllParameterFromSQL();
    }
}

void ProcessDataManager::saveData(int id, QString key, QString value)
{
    ParameterSQLDB Db;

    QString sql = "UPDATE ProcessParameter SET " + key + "='"+ value +"' WHERE ID = "+QString::number(id);

    qDebug()<<"Cpp:更新数据 sql:"<<sql;

    bool ok = Db.execSql(sql);

    if(!ok)
    {
        qDebug()<<"Cpp:更新数据失败";
    }
    else
    {
        qDebug()<<"Cpp:更新数据成功";
    }
}

void ProcessDataManager::searchData(QString find)
{
    qDebug()<<"Cpp:搜索数据"<<find;

    ParameterSQLDB Db;

    QSharedPointer<QSqlQuery> result;

    ParameterModel* itemModel = new ParameterModel();

    QString sql = "SELECT ID ,Name ,Number ,Knife ,Roller ,Stress01 ,Stress02 ,Stress03 ,F_Knife01 ,F_Knife02 ,F_Knife03 ,B_Knife01 ,B_Knife02 ,B_Knife03\
                  FROM ProcessParameter WHERE Name LIKE '%"+ find +"%' or Number LIKE '%" +find +"%'";

    qDebug()<<"Cpp:搜索数据 sql:"<<sql;

    result.reset(Db.execSql_result(sql));

    if(!result.isNull())
    {
        while(result->next())
        {
            ParameterItem* item = new ParameterItem();

            int id = result->value(0).toInt();
            item->setId(id);

            QString name = result->value(1).toString();
            item->setName(name);

            QString number = result->value(2).toString();
            item->setNumber(number);

            QString knife = result->value(3).toString();
            item->setKnife(knife);

            QString roller = result->value(4).toString();
            item->setRoller(roller);

            QString stress01 = result->value(5).toString();
            item->setStress01(stress01);

            QString stress02 = result->value(6).toString();
            item->setStress02(stress02);

            QString stress03 = result->value(7).toString();
            item->setStress03(stress03);

            QString f_knife01 = result->value(8).toString();
            item->setF_Knife01(f_knife01);

            QString f_knife02 = result->value(9).toString();
            item->setF_Knife02(f_knife02);

            QString f_knife03 = result->value(10).toString();
            item->setF_Knife03(f_knife03);

            QString b_knife01 = result->value(11).toString();
            item->setB_Knife01(b_knife01);

            QString b_knife02 = result->value(12).toString();
            item->setB_Knife02(b_knife02);

            QString b_knife03 = result->value(13).toString();
            item->setB_Knife03(b_knife03);

            itemModel->addItem(item);
        }

        emit getAllParameter_Signal(itemModel);

        result.clear();
    }
}
